package com.example.myapplicationshiyan2;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView imageView;
    private Button startPicBtn, stopPicBtn, lastPicBtn, nextPicBtn, syncPlayBtn;
    private Button startAudio, pauseAudio, stopAudio, replayAudio;
    private int count = 0;
    private int[] imgRes = { R.drawable.leaf1, R.drawable.leaf2, R.drawable.leaf3, R.drawable.pic };

    private Animation animation;

    private MediaPlayer mediaPlayer;
    private SeekBar seekBar;
    private boolean playFlag, pauseFlag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 绑定 ImageView ：
        imageView = findViewById(R.id.imageView);
        // 绑定操作 Pic 的四个按钮：
        startPicBtn = findViewById(R.id.startPic);
        lastPicBtn = findViewById(R.id.lastPic);
        nextPicBtn = findViewById(R.id.nextPic);
        stopPicBtn = findViewById(R.id.stopPic);

        // 音乐部分：
        seekBar = (SeekBar)this.findViewById(R.id.seekBar);
        mediaPlayer = MediaPlayer.create(this, R.raw.my_music);

        startAudio = findViewById(R.id.startAudio);
        pauseAudio = findViewById(R.id.pauseAudio);
        stopAudio = findViewById(R.id.stopAudio);
        replayAudio = findViewById(R.id.replayAudio);

        // 绑定同步播放按钮：
        syncPlayBtn = findViewById(R.id.syncPlay);


        startPicBtn.setOnClickListener(this);
        lastPicBtn.setOnClickListener(this);
        nextPicBtn.setOnClickListener(this);
        stopPicBtn.setOnClickListener(this);

        syncPlayBtn.setOnClickListener(this);

        startAudio.setOnClickListener(this);
        pauseAudio.setOnClickListener(this);
        stopAudio.setOnClickListener(this);
        replayAudio.setOnClickListener(this);

        // 通过 Bitmap 对象实现图片显示：
        Bitmap bm = BitmapFactory.decodeResource(this.getResources(), imgRes[count]);
        imageView.setImageBitmap(bm);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.startPic:
                // 设置图片资源：
                imageView.setImageResource(R.drawable.my_animation);
                // 获取动画对象：
                AnimationDrawable startPic = (AnimationDrawable) imageView.getDrawable();
                // 开启：
                startPic.start();
                break;
            case R.id.lastPic:
                animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.my_alpha);
                imageView.startAnimation(animation);
                if (count != 0){ count = count - 1; }
                Bitmap bm = BitmapFactory.decodeResource(MainActivity.this.getResources(), imgRes[count]);
                imageView.setImageBitmap(bm);
                break;
            case R.id.nextPic:
                animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.my_alpha);
                imageView.startAnimation(animation);
                count++;
                bm = BitmapFactory.decodeResource(MainActivity.this.getResources(), imgRes[count]);
                if (count == 3){ count = 0; }
                imageView.setImageBitmap(bm);
                break;
            case R.id.stopPic:
                // 获取动画对象：
                AnimationDrawable stopPic = (AnimationDrawable) imageView.getDrawable();
                // 关闭动画：
                stopPic.stop();
                imageView.stopNestedScroll();
                break;
            case R.id.syncPlay:
                animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.my_alpha);
                imageView.startAnimation(animation);
                // 音乐部分：
                if (pauseFlag == false) {
                    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            playFlag = false;
                            mp.release();
                        }
                    });
                    seekBar.setMax(mediaPlayer.getDuration());
                    playFlag = true;
                    UpdateSeekBar updateProgress = new UpdateSeekBar();
                    updateProgress.execute(1000);
                    seekBar.setOnSeekBarChangeListener(new MySeekBarChangeListener());
                    try {
                        if (mediaPlayer != null) {
                            mediaPlayer.stop();
                        }
                        mediaPlayer.prepare();
                        mediaPlayer.start();

                    } catch (Exception e) {
                    }
                } else {
                    pauseFlag = false;
                    playFlag = true;
                    mediaPlayer.start();
                    UpdateSeekBar updateProgress = new UpdateSeekBar();
                    updateProgress.execute(1000);
                    seekBar.setOnSeekBarChangeListener(new MySeekBarChangeListener());
                }

                // 设置图片资源：
                imageView.setImageResource(R.drawable.my_animation);
                // 获取动画对象：
                AnimationDrawable syncPlay = (AnimationDrawable) imageView.getDrawable();
                // 开启：
                syncPlay.start();
                break;
            case R.id.startAudio:
                if (pauseFlag == false) {
                    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            playFlag = false;
                            mp.release();
                        }
                    });
                    seekBar.setMax(mediaPlayer.getDuration());
                    playFlag = true;
                    UpdateSeekBar updateProgress = new UpdateSeekBar();
                    updateProgress.execute(1000);
                    seekBar.setOnSeekBarChangeListener(new MySeekBarChangeListener());
                    try {
                        if (mediaPlayer != null) {
                            mediaPlayer.stop();
                        }
                        mediaPlayer.prepare();
                        mediaPlayer.start();

                    } catch (Exception e) {
                    }
                } else {
                    pauseFlag = false;
                    playFlag = true;
                    mediaPlayer.start();
                    UpdateSeekBar updateProgress = new UpdateSeekBar();
                    updateProgress.execute(1000);
                    seekBar.setOnSeekBarChangeListener(new MySeekBarChangeListener());
                }
                break;
            case R.id.pauseAudio:
                if(mediaPlayer.isPlaying())
                {
                    playFlag = false;
                    pauseFlag=true;
                    mediaPlayer.pause();
                }
                break;
            case R.id.stopAudio:
                if(mediaPlayer.isPlaying()){
                    mediaPlayer.stop();
                    playFlag = false;
                }
                break;
            case R.id.replayAudio:
                if(mediaPlayer.isPlaying()){
                    playFlag = true;
                    mediaPlayer.seekTo(0);
                }
                break;
        }
    }

    private class UpdateSeekBar extends AsyncTask<Integer,Integer,String> {
        @Override
        protected void onPostExecute(String s) {
        }
        @Override
        protected void onProgressUpdate(Integer... values) {
            seekBar.setProgress(values[0]);
        }
        @Override
        protected String doInBackground(Integer... params) {
            while(playFlag){
                try {
                    Thread.sleep(params[0]);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                this.publishProgress(mediaPlayer.getCurrentPosition());
            }
            return null;
        }
    }

    private class MySeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            mediaPlayer.seekTo(seekBar.getProgress());
        }
    }
}